#include "app_store.h"
#include "user_mb_app.h"
#include "data_convert.h"
pid_cache_t pid_cache = { 0 };
static uint16_t buf[2] = { 0 };

void app_store_init() {

	pid_cache.kp = 1;
	pid_cache.ki = 1;
	pid_cache.kd = 1;
	pid_cache.out_max = 1000;
	pid_cache.out_min = 1;
	pid_cache.sv = 20;
	pid_cache.foutput_value = 36;
	pid_cache.input_value = 0.0f;
	pid_cache.output_value = 36;
	pid_cache.output_dir = 0;
	pid_cache.delay_ms = 1000;

}

int app_store_get_save_param_state() {

	return Slave_station.usSRegHoldBuf[Save_Reg];
}
void app_store_set_save_param_state(int state) {

	Slave_station.usSRegHoldBuf[Save_Reg] = state;
}

int app_store_get_pid_ctrl_state() {

	return Slave_station.usSRegHoldBuf[Pid_Ctrl_Status_Reg];
}
void app_store_set_pid_ctrl_state(int state) {

	Slave_station.usSRegHoldBuf[Pid_Ctrl_Status_Reg] = state;
}
int app_store_get_pid_no_measure_state() {
	return Slave_station.usSRegHoldBuf[Pid_NO_Measure_Status_Reg];
}
void app_store_set_pid_no_measure_state(int state) {
	Slave_station.usSRegHoldBuf[Pid_NO_Measure_Status_Reg] = state;
}
int app_store_get__maintenance_state() {

	return Slave_station.usSRegHoldBuf[Maintenance_status_Reg];
}
void app_store_set__maintenance_state(int state) {

	Slave_station.usSRegHoldBuf[Maintenance_status_Reg] = state;
}

int app_store_get_calibration_density_state() {

	return Slave_station.usSRegHoldBuf[Calib_density_Status_Reg];
}
void app_store_set_calibration_density_state(int state) {

	Slave_station.usSRegHoldBuf[Calib_density_Status_Reg] = state;
}

void app_store_set_calibration_viscosity_state(int state) {

	Slave_station.usSRegHoldBuf[Calib_viscosity_Status_Reg] = state;

}
int app_store_get_calibration_viscosity_state() {

	return Slave_station.usSRegHoldBuf[Calib_viscosity_Status_Reg];
}

int app_store_get_stabilize_value_state() {
	return Slave_station.usSRegHoldBuf[StabilizeValue_Status_Reg];
}
void app_store_set_stabilize_value_state(int state) {

	Slave_station.usSRegHoldBuf[StabilizeValue_Status_Reg] = state;

}
int app_store_get_pwm_output_value() {
	return Slave_station.usSRegHoldBuf[PWMoutput_Reg];
}
void app_store_set_pwm_output_value(uint16_t value) {

	Slave_station.usSRegHoldBuf[PWMoutput_Reg] = value;

}
int app_store_get_pwm_output_dir() {

	return Slave_station.usSRegHoldBuf[PWMDir_Reg];
}
void app_store_set_pwm_output_dir(uint16_t value) {

	Slave_station.usSRegHoldBuf[PWMDir_Reg] = value;
}
void app_store_set_pid_param_to_slave() {

	Slave_station.usSRegHoldBuf[0] = 0x01;

	Slave_station.usSRegHoldBuf[PIDcal_time_ms_Reg] = pid_cache.delay_ms;

	floatToUint16(pid_cache.sv, buf);
	Slave_station.usSRegHoldBuf[Setpoint_Reg] = buf[0];
	Slave_station.usSRegHoldBuf[Setpoint_Reg + 1] = buf[1];

	floatToUint16(pid_cache.kd, buf);
	Slave_station.usSRegHoldBuf[Kp_Reg] = buf[0];
	Slave_station.usSRegHoldBuf[Kp_Reg + 1] = buf[1];

	floatToUint16(pid_cache.ki, buf);
	Slave_station.usSRegHoldBuf[Ki_Reg] = buf[0];
	Slave_station.usSRegHoldBuf[Ki_Reg + 1] = buf[1];

	floatToUint16(pid_cache.kd, buf);
	Slave_station.usSRegHoldBuf[Kd_Reg] = buf[0];
	Slave_station.usSRegHoldBuf[Kd_Reg + 1] = buf[1];

	floatToUint16(pid_cache.out_max, buf);
	Slave_station.usSRegHoldBuf[Outmax_Reg] = buf[0];
	Slave_station.usSRegHoldBuf[Outmax_Reg + 1] = buf[1];

	floatToUint16(pid_cache.out_min, buf);
	Slave_station.usSRegHoldBuf[Outmin_Reg] = buf[0];
	Slave_station.usSRegHoldBuf[Outmin_Reg + 1] = buf[1];

}
void app_store_set_pid_param_from_slave() {

	pid_cache.delay_ms = Slave_station.usSRegHoldBuf[PIDcal_time_ms_Reg];

	buf[0] = Slave_station.usSRegHoldBuf[Setpoint_Reg];
	buf[1] = Slave_station.usSRegHoldBuf[Setpoint_Reg + 1];
	pid_cache.sv = uint16ToFloat(buf);

	buf[0] = Slave_station.usSRegHoldBuf[Kp_Reg];
	buf[1] = Slave_station.usSRegHoldBuf[Kp_Reg + 1];
	pid_cache.kp = uint16ToFloat(buf);

	buf[0] = Slave_station.usSRegHoldBuf[Ki_Reg];
	buf[1] = Slave_station.usSRegHoldBuf[Ki_Reg + 1];
	pid_cache.ki = uint16ToFloat(buf);

	buf[0] = Slave_station.usSRegHoldBuf[Kd_Reg];
	buf[1] = Slave_station.usSRegHoldBuf[Kd_Reg + 1];
	pid_cache.kd = uint16ToFloat(buf);

	buf[0] = Slave_station.usSRegHoldBuf[Outmax_Reg];
	buf[1] = Slave_station.usSRegHoldBuf[Outmax_Reg + 1];
	pid_cache.out_max = uint16ToFloat(buf);

	buf[0] = Slave_station.usSRegHoldBuf[Outmin_Reg];
	buf[1] = Slave_station.usSRegHoldBuf[Outmin_Reg + 1];
	pid_cache.out_min = uint16ToFloat(buf);

}
void app_store_set_density_value(uint16_t *value) {

	Slave_station.usSRegHoldBuf[Density_Reg] = value[0];
	Slave_station.usSRegHoldBuf[Density_Reg + 1] = value[1];
}
void app_store_set_temperature_value(uint16_t *value) {

	Slave_station.usSRegHoldBuf[Temperature_Reg] = value[0];
	Slave_station.usSRegHoldBuf[Temperature_Reg + 1] = value[1];
}
void app_store_set_dynviscosity_value(uint16_t *value) {

	Slave_station.usSRegHoldBuf[DynViscosity_Reg] = value[0];
	Slave_station.usSRegHoldBuf[DynViscosity_Reg + 1] = value[1];
}
void app_store_set_kinviscosity_value(uint16_t *value) {

	Slave_station.usSRegHoldBuf[KinViscosity_Reg] = value[0];
	Slave_station.usSRegHoldBuf[KinViscosity_Reg + 1] = value[1];

}
void app_store_get_stddensity_value(uint16_t *value) {
	value[0] = Slave_station.usSRegHoldBuf[StdDensity_Reg];
	value[1] = Slave_station.usSRegHoldBuf[StdDensity_Reg + 1];
}
void app_store_get_stdviscosity_value(uint16_t *value) {
	value[0] = Slave_station.usSRegHoldBuf[StdViscosity_Reg];
	value[1] = Slave_station.usSRegHoldBuf[StdViscosity_Reg + 1];
}
